#include "system.h"

extern void divide_error();
extern void debug();
extern void nmi();
extern void breakpoint();
extern void overflow();
extern void bounds_check();
extern void invalid_opcode();
extern void device_not_available();
extern void double_fault();
extern void coprocessor_segment_overrun();
extern void invalid_tss();
extern void segment_not_present();
extern void stack_segment();
extern void general_protection();
extern void reserved();

extern void printk(char*);

void die(char* str, long esp_ptr, long error_code)
{

}

void do_divide_error(long esp, long error_code)
{
	die("(0)divide", esp, error_code);
}

void do_debug(long esp, long error_code)
{
	die("(1)debug", esp, error_code);
}

void do_nmi(long esp, long error_code)
{
	die("(2)nmi", esp, error_code);
}

void do_breakpoint(long* esp, long error_code, 
		long fs, long es, long ds, long ebp,
		long esi, long edi, long edx, long ecx, long eax)
{

}

void do_overflow(long esp, long error_code)
{
	die("(4)overflow", esp, error_code);
}

void do_bounds_check(long esp, long error_code)
{
	die("(5)bounds check", esp, error_code);
}

void do_invalid_opcode(long esp, long error_code)
{
	die("(6)invalid opcode", esp, error_code);
}

//void do_device_not_available(long esp, long error_code)
//{
//	die("(7)device not available", esp, error_code);
//}

void do_double_fault(long esp, long error_code)
{
	die("(8)double fault", esp, error_code);
}

void do_coprocessor_segment_overrun(long esp, long error_code)
{
	die("(9)coprocessor segment overrun", esp, error_code);
}

void do_invalid_tss(long esp, long error_code)
{
	die("(10)invalid tss", esp, error_code);
}

void do_segment_not_present(long esp, long error_code)
{
	die("(11)segment not present", esp, error_code);
}

void do_stack_segment(long esp, long error_code)
{
	die("(12)stack segment", esp, error_code);
}

void do_general_protection(long esp, long error_code)
{
	die("(13)general protection", esp, error_code);
}

//void do_reserved(long esp, long error_code)
//{
//	die("(15)reserved", esp, error_code);
//}


void trap_init()
{
	set_trap_gate(0, &divide_error);
	set_trap_gate(1, &debug);
	set_trap_gate(2, &nmi);
	set_user_trap_gate(3, &breakpoint);
	set_user_trap_gate(4, &overflow);
	set_user_trap_gate(5, &bounds_check);
	set_trap_gate(6, &invalid_opcode);
	//set_trap_gate(7, &device_not_available);
	set_trap_gate(8, &double_fault);
	set_trap_gate(9, &coprocessor_segment_overrun);
	set_trap_gate(10, &invalid_tss);
	set_trap_gate(11, &segment_not_present);
	set_trap_gate(12, &stack_segment);
	set_trap_gate(13, &general_protection);
	//set_trap_gate(15, &reserved);
	
	printk("** trap OK **  ");
}

